package com.example;


import java.util.Arrays;
import java.util.StringJoiner;
import java.util.stream.Collectors;

class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
}
class Solution {
    public Node treeToDoublyList(Node root) {
        if (root == null) return null;

        Node L = treeToDoublyList(root.left);
        Node R = treeToDoublyList(root.right);

        if (L != null) {
            if (L.left == null) {
                L.right = root;
                root.left = L;
            } else {
                L.left.right = root;
                root.left = L.left;
            }
        }
        if (R != null) {
            R.left = root;
            root.right = R;
            if (L != null) {
                if (R.right == null) {
                    L.left = R;
                    R.right = L;
                } else {
                    L.left = R.right;
                    R.right.right = L;
                }
            } else {
                root.left = R;
                R.right = root;
            }
        } else if (L != null) {
            L.left = root;
            root.right = L;
        }
        return L != null ? L : root;
    }

    public String minNumber(int[] nums) {

        return Arrays.stream(nums).mapToObj(String::valueOf)
                .sorted((e1, e2) -> {
                    if (e1.equals(e2)) return 0;
                    char lastEq = 0;
                    for (int i = 0; i < e1.length() && i < e2.length(); i++) {
                        char e1c = e1.charAt(i);
                        char e2c = e2.charAt(i);
                        if (e1c > e2c) {
                            return 1;
                        } else if (e1c < e2c) {
                            return -1;
                        } else {
                            lastEq = e1c;
                        }
                    }
                    String longOne = e1.length() > e2.length() ? e1 : e2;
                    for (int i = (longOne.equals(e1) ? e2 : e1).length(); i < longOne.length(); i++) {
                        if (longOne.charAt(i) > lastEq) {
                            return longOne.equals(e1) ? 1 : -1;
                        } else if (longOne.charAt(i) < lastEq) {
                            return longOne.equals(e1) ? -1 : 1;
                        }
                    }
                    if (e2.length() > e1.length()) {
                        for (int i = e1.length(); i < e2.length(); i++) {
                            if (e2.charAt(i) > lastEq) {
                                return 1;
                            } else if (e2.charAt(i) < lastEq) {
                                return -1;
                            }
                        }
                    }
                    return 0;
                }).collect(Collectors.joining());
    }

    public static void main(String[] args) {
        String[] case1 = {"31", "3"};
        String[] case2 = {"33", "3"};
        Arrays.sort(case2, (e1, e2) -> {
            if (e1.equals(e2)) return 0;
            char lastEq = 0;
            for (int i = 0; i < e1.length() && i < e2.length(); i++) {
                char e1c = e1.charAt(i);
                char e2c = e2.charAt(i);
                if (e1c > e2c) {
                    return 1;
                } else if (e1c < e2c) {
                    return -1;
                } else {
                    lastEq = e1c;
                }
            }
            String longOne = e1.length() > e2.length() ? e1 : e2;
            for (int i = (longOne.equals(e1) ? e2 : e1).length(); i < longOne.length(); i++) {
                if (longOne.charAt(i) > lastEq) {
                    return longOne.equals(e1) ? 1 : -1;
                } else if (longOne.charAt(i) < lastEq) {
                    return longOne.equals(e1) ? -1 : 1;
                }
            }
            if (e2.length() > e1.length()) {
                for (int i = e1.length(); i < e2.length(); i++) {
                    if (e2.charAt(i) > lastEq) {
                        return 1;
                    } else if (e2.charAt(i) < lastEq) {
                        return -1;
                    }
                }
            }
            return 0;
        });
        System.out.println(Arrays.toString(case2));

    }

}